package com.rongc.client.freight.business.carrier.view.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.view.widget.BGASortableNinePhotoLayoutView;
import com.rongc.client.freight.business.carrier.view.fragment.ConveyanceDetailFragment;
import com.rongc.client.freight.business.supply.model.SupplyBean;
import com.rongc.client.freight.business.supply.view.fragment.SupplyCommentListFragment;
import com.viewpagerindicator.TabPageIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import cn.bingoogolapple.photopicker.activity.BGAPhotoPreviewActivity;

/**
 * Created by Bruce on 2017/7/7.
 */
public class ConveyanceSupplyDetailActivity extends BaseActivity implements View.OnClickListener, BGASortableNinePhotoLayoutView.Delegate{
    private static final String[] CONTENT = new String[] { "车辆详情", "货主评价"};

    @Bind(R.id.pager)
    ViewPager pager;
    @Bind(R.id.indicator)
    TabPageIndicator indicator ;

    FragmentPagerAdapter adapter;
    ConveyanceDetailFragment mYJSFragment;
    SupplyCommentListFragment mWJSFragment;
    List<Fragment> mFragments = new ArrayList<Fragment>();

    String waybillId;
    SupplyBean supplyBean;

    @Bind(R.id.tv_departure)
    TextView mTvDeparture;
    @Bind(R.id.tv_destination)
    TextView mTvDestination;
    @Bind(R.id.tv_name)
    TextView mTvName;
    @Bind(R.id.tv_count)
    TextView mTvCount;

    @OnClick({R.id.btn_submit})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_submit:
                ActivityUtils.toast("派单");
                break;
        }
    }

    @Override
    public void onClickNinePhotoItem(BGASortableNinePhotoLayoutView ninePhotoLayout, View view, int position, String model, List<String> models) {
        startActivity(BGAPhotoPreviewActivity.newIntent(this, null, (ArrayList<String>) models, position));
    }

    @Override
    public boolean onLongClickNinePhotoItem(BGASortableNinePhotoLayoutView ninePhotoLayout, View view, int position, String model, List<String> models) {
        return false;
    }

    void makeTopView(SupplyBean supplyBean){
        mTvDeparture.setText(supplyBean.getStartPlace() + " - " +supplyBean.getEndPlace());
        mTvDestination.setText(supplyBean.getFreight());
        mTvName.setText(supplyBean.getLoadTime());
        mTvCount.setText(supplyBean.getCarModels());
    }

    @Override
    public void initParams() {
        waybillId = getIntent().getStringExtra("data");
    }


    @Override
    public void initView() {
        initToolbar(R.string.conveyance_supply_detail_title);

        if(mFragments != null && mFragments.size() == 0){
            mYJSFragment = ConveyanceDetailFragment.getInstance();
            mWJSFragment = SupplyCommentListFragment.getInstance();
            mFragments.add(mYJSFragment);
            mFragments.add(mWJSFragment);
        }

        adapter = new GoogleMusicAdapter(getSupportFragmentManager());

        pager.setAdapter(adapter);
        pager.setOffscreenPageLimit(2);

        indicator.setVisibility(View.VISIBLE);
        indicator.setViewPager(pager);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_conveyance_supply_detail;
    }

    class GoogleMusicAdapter extends FragmentPagerAdapter {
        public GoogleMusicAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    if(mYJSFragment == null)
                        mYJSFragment = ConveyanceDetailFragment.getInstance();
                    return mYJSFragment;
                case 1:
                    if(mWJSFragment == null) {
                        mWJSFragment = SupplyCommentListFragment.getInstance();
                    }
                    return mWJSFragment;
                default:
                    break;
            }

            return mFragments.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return CONTENT[position % CONTENT.length].toUpperCase();
        }

        @Override
        public int getCount() {
            return CONTENT.length;
        }
    }
}
